From e1ba760fd04c1c94d05d4fa8069110c606786cbc Mon Sep 17 00:00:00 2001 From: Jake Goulding Date: Fri, 15 Jul 2016 16:55:29 -0400 Subject: [PATCH] Allow moving files into the future as well --- tests/cargotest/support/paths.rs | 38 +++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/tests/cargotest/support/paths.rs b/tests/cargotest/support/paths.rs index 62e3b548d..bf3b6d446 100644 --- a/tests/cargotest/support/paths.rs +++ b/tests/cargotest/support/paths.rs @@ -58,7 +58,17 @@ pub fn home() -> PathBuf { pub trait CargoPathExt { fn rm_rf(&self); fn mkdir_p(&self); - fn move_into_the_past(&self); + + fn move_into_the_past(&self) { + self.move_in_time(|sec, nsec| (sec - 3600, nsec)) + } + + fn move_into_the_future(&self) { + self.move_in_time(|sec, nsec| (sec + 3600, nsec)) + } + + fn move_in_time(&self, F) + where F: Fn(u64, u32) -> (u64, u32); } impl CargoPathExt for Path { @@ -91,31 +101,37 @@ impl CargoPathExt for Path { }) } - fn move_into_the_past(&self) { + fn move_in_time(&self, travel_amount: F) + where F: Fn(u64, u32) -> ((u64, u32)), + { if self.is_file() { - time_travel(self); + time_travel(self, &travel_amount); } else { - recurse(self, &self.join("target")); + recurse(self, &self.join("target"), &travel_amount); } - fn recurse(p: &Path, bad: &Path) { + fn recurse(p: &Path, bad: &Path, travel_amount: &F) + where F: Fn(u64, u32) -> ((u64, u32)), + { if p.is_file() { - time_travel(p) + time_travel(p, travel_amount) } else if !p.starts_with(bad) { for f in t!(fs::read_dir(p)) { let f = t!(f).path(); - recurse(&f, bad); + recurse(&f, bad, travel_amount); } } } - fn time_travel(path: &Path) { + fn time_travel(path: &Path, travel_amount: &F) + where F: Fn(u64, u32) -> ((u64, u32)), + { let stat = t!(path.metadata()); let mtime = FileTime::from_last_modification_time(&stat); - let newtime = mtime.seconds_relative_to_1970() - 3600; - let nanos = mtime.nanoseconds(); - let newtime = FileTime::from_seconds_since_1970(newtime, nanos); + + let (sec, nsec) = travel_amount(mtime.seconds_relative_to_1970(), mtime.nanoseconds()); + let newtime = FileTime::from_seconds_since_1970(sec, nsec); // Sadly change_file_times has a failure mode where a readonly file // cannot have its times changed on windows. -- 2.30.2